package com.etime.shuhuiyuan.services.department.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.dao.DepartmentDao;
import com.etime.shuhuiyuan.pojo.Demo;
import com.etime.shuhuiyuan.pojo.Department;
import com.etime.shuhuiyuan.services.department.interfaces.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public int insertDepartment(Department department) {
        return departmentDao.insert(department);
    }

    @Override
    public int updateDepartment(Department department) {
        return departmentDao.updateById(department);
    }

    @Override
    public List<Department> selectDepartmentList(Department department) {
        //框架自带的查询结果集
        QueryWrapper<Department> wrapper = new QueryWrapper();
        //查询条件:非空限制
        if (department!=null && department.getDeptname()!=null && !department.getDeptname().equalsIgnoreCase("")){
            //eq方法相当于给查询结果的列名赋值department.getDepatname
            wrapper.eq("deptname",department.getDeptname());
        }
        if (department!=null && department.getDeptusername()!=null && department.getDeptusername().equalsIgnoreCase("")){
            wrapper.eq("deptusername",department.getDeptusername());
        }
        if (department!=null && department.getDeptstate()!=null && department.getDeptstate()!=-1){
            wrapper.eq("deptstate",department.getDeptstate());
        }
        return departmentDao.selectList(wrapper);
    }

    @Override
    public Page<Department> selectDepartmentByPage(Department department, long pagenum, long pagesize) {
        //分页参数
        Page<Department> page = new Page<>(pagenum,pagesize);
        //查询条件
        QueryWrapper<Department> wrapper = new QueryWrapper<>();

        if (department!=null && department.getDeptname()!=null && !department.getDeptname().equalsIgnoreCase("")){
            //eq方法相当于给查询结果的列名赋值department.getDepatname
            wrapper.eq("deptname",department.getDeptname());
        }
        if (department!=null && department.getDeptusername()!=null && department.getDeptusername().equalsIgnoreCase("")){
            wrapper.eq("deptusername",department.getDeptusername());
        }
        if (department!=null && department.getDeptstate()!=null && department.getDeptstate()!=-1){
            wrapper.eq("deptstate",department.getDeptstate());
        }

        return departmentDao.selectPage(page,wrapper);
    }


    @Override
    public Department selectDepartmentByid(Integer deptid) {
        return departmentDao.selectById(deptid);
    }
}
